<template>
	<view>
		
		<view class="" style="background-color: #FFFFFF;margin: 10px;border-radius: 10px;padding: 10px;">

		    <form >
				<view class="uni-form-item uni-column row-center">
					<view class="title">乘客类型</view>
					<view>
						<radio-group @change="radioChange">
							<label style="margin-right: 12px;">
								<radio :value="1"  checked="true" /><text>成人</text>
							</label>
							<label>
								<radio :value="2" /><text>儿童</text>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="uni-form-item uni-column row-center">
					<view class="title">乘客姓名</view>
					<input  class="uni-input" style="flex:1" v-model="passengerForm.name"  name="input" placeholder="请输入姓名" />
				</view>
				<view class="uni-form-item uni-column row-center">
					<view class="title">身份证号</view>
					<input type="idcard" style="flex:1" class="uni-input" v-model="passengerForm.idCard"  name="input" placeholder="请输入身份证号" />
				</view>
				<view class="uni-form-item uni-column row-center">
					<view class="title">手机号码</view>
					<input type="number" style="flex:1" maxlength="11" class="uni-input" v-model="passengerForm.phone"  name="input" placeholder="请输入手机号" />
				</view>
			</form>
		
		</view>
	
	<view @click="savePassenger" style="border-radius: 20px;height: 38px;line-height: 38px;color: #FFFFFF;margin: 20px;background-color: #007AFF;text-align: center;">
		<view>保存</view>
	</view>
		
		
	</view>
</template>


<script>

    import {addPassenger} from "@/api/user"
	var util = require('@/common/js/util.js');
	var _getDiffDate = util.getDiffDate;
	var _addDay = util.addDay;
	var _dateDiff = util.dateDiff;
	var _parseDate = util.parseDate;
	var _formatDate = util.formatDate
	var _validIdCard = util.validIdCard
	var _getAge = util.getAge
	export default {
		data() {
			return {
				passengerForm: {
					id:'',
					type: 1,
					name: '',
					phone: '',
					idCard:''
				},
		}
		},
		onLoad(option) {	
			this.passengerForm = {
				id: option.id?option.id:'',
				type: option.type?option.type:1,
				name: option.name?option.name:'',
				phone: option.phone? option.phone:'',
				idCard: option.idCard?option.idCard:''
			}
		},
		onReady() {
		},
		methods:{
			 radioChange: function(evt) {
				this.passengerForm.type = evt.detail.value
			},
			
			savePassenger:function(){
				console.log(this.passengerForm)
				
				
				if(!this.passengerForm.name){
					return this.$util.Tips({title: "请填写姓名"})
				}
				if(this.passengerForm.name.length < 2){
					return this.$util.Tips({title: "姓名不能小于2个字"})
				}
				if(this.passengerForm.name.length > 32){
					return this.$util.Tips({title: "姓名不能大于32个字"})
				}
				if(!this.passengerForm.phone){
					return this.$util.Tips({title: "请输入手机号"})
				}
				if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.passengerForm.phone)) return this.$util.Tips({
					title: '请输入正确的手机号码'
				});
				if(!_validIdCard(this.passengerForm.idCard)){
					return this.$util.Tips({title: "身份证号不正确"})
				}
				if(this.passengerForm.type==2&&_getAge(this.passengerForm.idCard)>=7){
					return uni.showModal({
					    title: '温馨提示',
					    content: '当前儿童年龄不符合相关规定，请修改【乘客类型】成人',
						showCancel: false,
						confirmText: '我已知晓',
					    success: function (res) {
					        if (res.confirm) {
					            console.log('用户点击确定');
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});					
				}
				console.log('验证通过');
				uni.showLoading({
					title:"请稍后"
				})
				addPassenger(this.passengerForm).then(res => {
					uni.hideLoading()
					var pages = getCurrentPages();
					var prevPage = pages[pages.length - 2]; //上一个页面
					//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
					//uni或者vue用$vm
					prevPage.$vm.isRefresh = true
					uni.navigateBack({//返回
						delta: 1
					})
				}).catch(err => {
					uni.hideLoading();
					return this.$util.Tips({
						title: err
					});
				});
				
			}
			
		 }
		
	}
</script>

<style >
	.uni-form-item .title {
		padding: 28rpx 20rpx ;
	}
</style>
